package jp.co.nes.awssupport.test.utility;

import java.util.ArrayList;
import java.util.List;

import jp.co.nes.awssupport.common.bean.MonthlyCompiledBillingBean;

/**
 * 月次集計結果アサーションヘルパーです。
 */
public class AssertMonthlyCompiledBillingHelper extends AbstractAssertHelper<MonthlyCompiledBillingBean> {

    /** 月次集計結果の件数を検索する際に使用するSelect文 */
    private static final String SEL_COUNT = "select count(*) as Count from monthly_compiled_billing";

    /** 月次集計結果を検索する際に使用するSelect文 */
    private static final String SEL = ""
            + "select LinkedAccountId,"
            + "       YearMonth,"
            + "       ProductName,"
            + "       UsageType,"
            + "       Rate,"
            + "       UsageQuantity,"
            + "       BillingAmount,"
            + "       Description,"
            + "       Unit"
            + "  from monthly_compiled_billing"
            + " where LinkedAccountId = ?"
            + "   and YearMonth = ?"
            + "   and ProductName = ?"
            + "   and UsageType = ?"
            + "   and Rate = ?";

    /**
     * デフォルトコンストラクタです。
     */
    public AssertMonthlyCompiledBillingHelper() {
        super(MonthlyCompiledBillingBean.class);
    }

    /*
     * (非 Javadoc)
     * @see jp.co.nes.awssupport.test.utility.AbstractAssertHelper#getSelectCountQuery()
     */
    @Override
    protected String getSelectCountQuery() {
        return SEL_COUNT;
    }

    /*
     * (非 Javadoc)
     * @see jp.co.nes.awssupport.test.utility.AbstractAssertHelper#getSelectBeanQuery()
     */
    @Override
    protected String getSelectBeanQuery() {
        return SEL;
    }

    /*
     * (非 Javadoc)
     * @see jp.co.nes.awssupport.test.utility.AbstractAssertHelper#toConditionList(jp.co.nes.awssupport.common.bean.BaseBean)
     */
    @Override
    protected List<Object> toConditionList(MonthlyCompiledBillingBean bean) {
        List<Object> conditionList = new ArrayList<>();
        conditionList.add(bean.getLinkedAccountId());
        conditionList.add(bean.getYearMonth());
        conditionList.add(bean.getProductName());
        conditionList.add(bean.getUsageType());
        conditionList.add(bean.getRate());
        return conditionList;
    }

    /*
     * (非 Javadoc)
     * @see jp.co.nes.awssupport.test.utility.AbstractAssertHelper#isMatch(jp.co.nes.awssupport.common.bean.BaseBean, jp.co.nes.awssupport.common.bean.BaseBean)
     */
    @Override
    protected boolean isMatch(MonthlyCompiledBillingBean actualBean, MonthlyCompiledBillingBean expectedBean) {
        if (actualBean == null) {
            if (expectedBean == null) {
                return true;
            } else {
                return false;
            }
        } else {
            if (expectedBean == null) {
                return false;
            }
        }

        StringBuilder actual = new StringBuilder();
        actual.append(actualBean.getLinkedAccountId());
        actual.append(actualBean.getYearMonth());
        actual.append(actualBean.getProductName());
        actual.append(actualBean.getUsageType());
        actual.append(actualBean.getRate());

        StringBuilder expected = new StringBuilder();
        expected.append(expectedBean.getLinkedAccountId());
        expected.append(expectedBean.getYearMonth());
        expected.append(expectedBean.getProductName());
        expected.append(expectedBean.getUsageType());
        expected.append(expectedBean.getRate());

        if (actual.toString().equals(expected.toString())) {
            return true;
        }
        return false;
    }
}
